<?php

use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;

class Groups extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $name;

    /**
     *
     * @var string
     */
    public $category;

    /**
     *
     * @var integer
     */
    public $privacy;

    /**
     *
     * @var string
     */
    public $icon;

    /**
     *
     * @var integer
     */
    public $creator_id;

    /**
     *
     * @var string
     */
    public $cover_photo;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     *
     * @var string
     */
    public $date_modified;
    public function validation()
    {
        if(!$this->icon) $this->icon = '1422600100/no-dp_wudgdv.jpg';
    }
    public function beforeValidationOnCreate()
    {
        $this->validate(new Uniqueness(
          array(
                "field"   => "name",
                "message" => "The group name is already registered please try another"
            )
        ));

        $this->date_created = CURR_DATE;
        

        if ($this->validationHasFailed() == true)
        {
            return false;
        }

        
    }
    public function listGroup($user_id)
    {
        $phql = "SELECT gt.* FROM Groups gt WHERE gt.creator_id = ?0 
                 ";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return @$data;   

    }
    public function beforeValidationOnUpdate()
    {
        $this->date_modified = CURR_DATE;
    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'name' => 'name', 
            'category' => 'category', 
            'privacy' => 'privacy', 
            'icon' => 'icon', 
            'creator_id' => 'creator_id', 
            'cover_photo' => 'cover_photo', 
            'date_created' => 'date_created', 
            'date_modified' => 'date_modified'
        );
    }
    public function countGroups($user_id)
    {
        $phql = "SELECT COUNT(g.id) as total 
                FROM Groups g
                LEFT JOIN GroupMembers gm on gm.group_id = g.id
                LEFT JOIN GroupOrganizers go on go.group_id = g.id
                WHERE g.creator_id = ?0 OR gm.user_id = ?0 OR go.user_id =?0
                GROUP BY g.id
                ";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        
        return (sizeof($data) > 0) ? $data[0]->total : 0 ;

    }
    public function listNames()
    {
        $phql = "SELECT id,name,icon,privacy FROM Groups";
        $data = $this->modelsManager->executeQuery($phql);
        return $data;
    }

    public function listSuggestedGroups($user_id)
    {
        $group_ids = array();

        $phql1 = "SELECT group_id FROM GroupMembers WHERE user_id = ?0";
        $data2 = $this->modelsManager->executeQuery($phql1,array(0=>$user_id));
        foreach ($data2 as $value) {
            $group_ids[] = $value->group_id;
        }
        $ids = implode(",", $group_ids);


        $condition = ($ids != '') ? "WHERE gt.id NOT IN (".$ids .")" : "";

        $phql = "SELECT gt.id,gt.name,gt.icon,COUNT(gm.user_id) as total_members FROM Groups gt
                LEFT JOIN  GroupMembers gm ON gm.group_id = gt.id 
                ".$condition.
                "
                GROUP BY gt.id
                ORDER BY gt.date_created DESC
                ";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return @$data;

    }

}
